package org.trade.pay.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.trade.common.api.IPayApi;
import org.trade.common.constant.TradeEnums;
import org.trade.common.protocol.pay.CallbackPaymentReq;
import org.trade.common.protocol.pay.CallbackPaymentRes;
import org.trade.common.protocol.pay.CreatePaymentReq;
import org.trade.common.protocol.pay.CreatePaymentRes;
import org.trade.pay.service.IPayService;

@Controller
public class PayApiImpl implements IPayApi {

	@Autowired
	private IPayService payService;

	@RequestMapping(value = "/createPayment", method = RequestMethod.POST)
	@ResponseBody
	public CreatePaymentRes createPayment(@RequestBody CreatePaymentReq createPaymentReq) {
		return payService.createPayment(createPaymentReq);
	}

	@ResponseBody
	@RequestMapping(value = "/callbackPayment", method = RequestMethod.POST)
	public CallbackPaymentRes callbackPayment(@RequestBody CallbackPaymentReq callbackPaymentReq) {
		CallbackPaymentRes callbackPaymentRes = new CallbackPaymentRes();
		try {
			callbackPaymentRes = payService.callbackPayment(callbackPaymentReq);
		} catch (Exception e) {
			callbackPaymentRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
			callbackPaymentRes.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
		}
		return callbackPaymentRes;
	}

}
